﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.Timers;
using System.Configuration;
using System.Web.Script.Serialization;
using XZL.Web.Biz;
using XZL.Web.Da;
using XZL.Web.Entity;

namespace XZL.Web.MVC.WXModel
{
    public class TaskAction
    {

        private static string content = "";
        /// <summary>
        /// 输出信息存储的地方.
        /// </summary>
        public static string Content
        {
            get { return TaskAction.content; }
            set { TaskAction.content += "<div>" + value + "</div>"; }
        }
        /// <summary>
        /// 定时器委托任务 调用的方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        public static void SetContent(object source, ElapsedEventArgs e)
        {
            if (DateTime.Now.ToString("HH:mm:ss") == "10:00:00")
            {
                //这里写你定时执行的代码
                List<Proc_CourseInfo_SelectStartListResult> ls = CourseBiz.GetStartCorseList();


                //获取token
                string appid = ConfigurationManager.AppSettings["appid"];
                string appsecret = ConfigurationManager.AppSettings["appsecret"];
                WebClient webclient = new WebClient();
                string url = @"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret + "";
                byte[] bytes = webclient.DownloadData(url);//在指定的path上下载
                string result = Encoding.GetEncoding("utf-8").GetString(bytes);//转string
                JavaScriptSerializer js = new JavaScriptSerializer();
                AccessToken amodel = js.Deserialize<AccessToken>(result);//此处为定义的类，用以将json转成model
                string a_token = amodel.access_token;

                for (int i = 0; i < ls.Count; i++)
                {
                    var data = WXTemplate.ClassWarn(ls[i].WeiXinName, "【" + ls[i].ItemName + "】", "【" + Convert.ToDateTime(ls[i].StartTime).ToString("yyyy-MM-dd HH:mm:ss") + "】", "如有任何问题，请联系客服：010-64756681 / 64758501。");

                    SendTemplateMessageService.SendTemplateMessage("3uTEmR4egH-6LNcz3wkHv8QqJihep4NfUlcPxvcGLxs", ls[i].WeiXinID, "wfdot.com/Course/CourseInfo?CourseID=" + ls[i].CourseID, a_token, data);
                }
            }
        }
        /// <summary>
        /// 应用池回收的时候调用的方法
        /// </summary>
        public static void SetContent()
        {
            Content = "END: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }
}